#include "toybricks.h"

ToyBricks::ToyBricks()
{
    _width = 0;
    _data.clear();
    _fill.clear();
}

ToyBricks::ToyBricks(quint8 row,quint8 col)
{
    if(row > 0 && col > 0){
        _width = col;
        _data.resize(row * col);
        _data.fill(0);

        _fill.resize(row);
        _fill.fill(0);
    }
    else{
        _width = 0;
        _data.clear();
        _fill.clear();
    }
}

ToyBricks::ToyBricks(quint8 array[],quint8 row,quint8 col,Qt::GlobalColor color)
{
    if(row > 0 && col > 0)
    {
        _fill.resize(row);
        _data.resize(row * col);
        _width = col;

        for(quint8 i = 0; i < row * col; i++)
            _data[i] = array[i];

        for(quint8 i = 0;i < row; i++){
            quint8 cnt = 0;
            for(quint8 j = 0; j < col;j++){
                _data[i * col + j] = array[i * col + j];

                if(_data[i * col + j] > 0){
                    cnt++;
                }
            }

            _fill[i] = cnt;
        }
        setColor(color);
    }
    else{
        _width = 0;
        _data.clear();
        _fill.clear();
    }
}

void ToyBricks::rotateLeft()
{
    if(size() > 0)
    {
        quint8 data[size()];

        for(quint8 row = 0; row < height(); row++) {
            for(quint8 col = 0; col < width(); col++)
                data[(width() -1 - col) * height() + row] = _data[row * width() + col];
        }

        for(quint8 i = 0; i < size(); i++)
            _data[i] = data[i];

        _width = size()/_width;
    }
}

void ToyBricks::clean()
{
    _width = 0;
    _data.clear();
}

ToyBricks ToyBricks::createTetrixPiece()
{
    static quint8 pa0[3 * 2] = {1,0,1,1,0,1};//SPiece
    static quint8 pa1[3 * 2] = {0,1,1,1,1,0};//ZPiece
    static quint8 pa2[3 * 2] = {1,0,1,0,1,1};//LPiece
    static quint8 pa3[3 * 2] = {0,1,0,1,1,1};//MirroredLPiece
    static quint8 pa4[4 * 1] = {1,1,1,1};//IPiece
    static quint8 pa5[2 * 3] = {1,1,1,0,1,0};//TPiece

    QTime t;
    quint8 i = t.secsTo(QTime::currentTime());
    qsrand(i);
    quint8 seq = qrand() % 6;

    if(seq == 0)
        return ToyBricks(pa0,3,2,Qt::green);
    else if(seq == 1)
        return ToyBricks(pa1,3,2,Qt::red);
    else if(seq == 2)
        return ToyBricks(pa2,3,2,Qt::yellow);
    else if(seq == 3)
        return ToyBricks(pa3,3,2,Qt::blue);
    else if(seq == 4)
        return ToyBricks(pa4,4,1,Qt::black);
    else
        return ToyBricks(pa5,2,3,Qt::magenta);
}

bool ToyBricks::isCollision(const ToyBricks &piece,const QPoint &point)
{
    qint8 pos_x = point.x();
    qint8 pos_y = point.y();


    //for(quint8 row = 0; row < piece.height();row++){
    for(quint8 row = (pos_y < 0 ? -1 * pos_y : 0); row < piece.height();row++){
        for(quint8 col = 0; col < piece.width(); col++){
            quint32 idx = _width * (pos_y + row) + pos_x + col;
            if(piece.isFilled(row,col) && _data[idx] > 0)
                return true;
        }
    }
    return false;
}

void ToyBricks::join(const ToyBricks &piece, const QPoint &point)
{
    for(quint8 row = 0; row < piece.height(); row++){
        for(quint8 col = 0; col < piece.width(); col++){
            if(piece.isFilled(row,col)){
                quint32 idx = (point.y() + row) * _width + (point.x() + col);
                _data[idx] = piece.getValue(row * piece.width() + col);

                _fill[point.y() + row]++;
            }
        }
    }
}

void ToyBricks::draw(QPainter &painter,const QPoint &pos,quint8 cell_size)
{
    QString text;
    for(quint8 row = 0; row < height(); row++){
        for(quint8 col = 0;col < width(); col++){
            //if(this->isFilled(row,col)){
                quint8 val = getValue(row * width() + col);
                painter.fillRect(pos.x() + col * cell_size + 1,pos.y() + row * cell_size + 1,cell_size - 1,cell_size - 1,val > 0 ? Qt::GlobalColor(val) : Qt::white);

                //text.setNum(val);
                //painter.drawText(col * cell_size + pos.x(),row * cell_size + pos.y() + 10,text);

            //}
        }
        /*
        if(width() > 10){
            text.setNum(this->getRowFilledCnt(row));
            painter.drawText(width() * cell_size + pos.x() + 10,row * cell_size + pos.y() + 10,text);
        }
        */
    }
}

void ToyBricks::setColor(quint8 idx,Qt::GlobalColor color)
{
    _data[idx] = color;
}

void ToyBricks::setColor(quint8 row,quint8 col,Qt::GlobalColor color)
{
    _data[row * _width + col] = color;
}

void ToyBricks::setColor(Qt::GlobalColor color)
{
    for(quint8 idx = 0; idx < size(); idx++)
        if(_data[idx] > 0)
            _data[idx] = color;
}
